<!--
/**
* Author: txj
* Date: 2023年10月19日10:53:40
* Desc: 会员卡列表
*/
-->
<template>
  <view class="main">
    <view class="list" :class="listDataLength ? 'content' : ''">
      <yy-list :list="listData" :loading="listLoading" :finished="listFinished" empty-type="card">
        <view @click="select(item)" v-for="(item, index) in listData" :key="index">
          <card-item
            :card="item"
            :selected="fromPage ? card && card.id === item.id : false"
            :show-detail="fromPage ? false : true"
            :show-recharge="fromPage ? false : true"
          ></card-item>
        </view>
      </yy-list>
    </view>
    <yy-button v-if="!fromPage" class="mg20" @click="addCard">添加会员卡</yy-button>
  </view>
</template>
<script>
  import listMixins from '@/mixins/listMixins'
  import { deDupArray } from '@/utils'
  import CardItem from './components/cardItem'
  import { locSelCard, locSelGoodsCoupon } from '@/utils/locStore/data'
  export default {
    name: 'card',
    components: { CardItem },
    mixins: [listMixins],
    data() {
      return {
        card: null,
        totalPrice: '',
        orderId: '',
        mallCommodityld: '',
        fromPage: ''
      }
    },
    onLoad(option) {
      console.log('option', option)
      this.fromPage = option.fromPage || ''
      if (this.fromPage) {
        uni.setNavigationBarTitle({ title: '选择会员卡' })
      } else {
        uni.setNavigationBarTitle({ title: '会员卡列表' })
      }
      if (this.fromPage) {
        this.totalPrice = option.totalPrice
        this.orderId = option.orderId
        this.mallCommodityId = option.mallCommodityId
        this.cinemaId = option.cinemaId
        if (option.shoppingCart) {
          const query = this.$utils.decode(option.shoppingCart)
          console.log('option', query)
          this.shoppingCart = JSON.stringify(query)
          // this.shoppingCart = option.shoppingCart
        }
      }
    },
    onShow() {
      this.listLoad()
      this.card = locSelCard.get() || null
      const reCard = uni.getStorageSync('reCard')
      console.log('reCard', reCard)
      if (reCard) {
        uni.removeStorageSync('reCard')
        this.$msg.toast('' + reCard)
        this.listRefresh()
      }
    },
    methods: {
      onloadList() {
        const query = {
          ...this.pageInfo
        }

        let postApi = uni.$api.memberCard.getCardList
        switch (this.fromPage) {
          case '':
            postApi = uni.$api.memberCard.getCardList
            break
          case 'sureOrder':
            postApi = uni.$api.memberCard.getMemberCardByOrderId
            query.orderId = this.orderId
            break
          case 'goodPay':
            postApi = uni.$api.memberCard.getMemberCardByMallCommodityId
            query.mallCommodityId = this.mallCommodityId
            query.cinemaId = this.cinemaId
            break
          case 'mallPay':
            postApi = uni.$api.memberCard.getMemberCardByFoodCart
            query.shoppingCart = this.shoppingCart
            query.cinemaId = this.cinemaId
            break

          default:
            break
        }
        return postApi(query).then((res) => {
          if (this.isLoading) {
            // 是下啦刷新 清空
            this.listData = []
          }
          const listData = [...this.listData, ...res.list]
          this.listData = deDupArray(listData, 'id')
          this.listData.forEach((item) => {
            if (Number(this.totalPrice) > Number(item.balance)) {
              item.unable = true
              item.noMoney = true
            } else {
              if (item.isExpire || item.errPasswordTip) {
                item.unable = true
              } else {
                item.unable = false
              }
            }
          })
          // this.$nextTick(() => {
          //   this.onReplace()
          // })
          return res
        })
      },
      onPullDownRefresh() {
        this.listRefresh()
      },
      onReachBottom() {
        this.listLoad()
      },
      getStatus(item) {
        if (item.isFrozen === '1') {
          return 'freeze'
        } else if (item.isOver === '1') {
          return 'overdue'
        } else if (item.isUse === '1') {
          return 'used'
        } else if (item.isVoid === '1') {
          return 'invalid'
        }
      },
      addCard() {
        this.$tools.navigateTo('/pagesOrder/card/addCard')
      },
      select(item) {
        if (item.unable || !item.canUse) {
          return
        }
        if (this.fromPage) {
          locSelCard.set(item)
          locSelGoodsCoupon.remove() //用完卡清券
          this.$tools.navigateTo(-1)
        }
      }
    },

    computed: {}
  }
</script>
<style lang="scss" scoped>
  .main {
    display: flex;
    flex-direction: column;
    overflow-x: hidden;
    overflow-y: auto;
    width: 100%;
    height: 100vh;
    background: #f3f5f7;
    .list {
      flex: 1;
      overflow-y: scroll;
    }
  }
</style>
